3 // Copyright (C) 2005 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32 // Permission to use, copy, modify, sell, and distribute this software
33 // is hereby granted without fee, provided that the above copyright
34 // notice appears in all copies, and that both that copyright notice and
35 // this permission notice appear in supporting documentation. None of
36 // the above authors, nor IBM Haifa Research Laboratories, make any
37 // representation about the suitability of this software for any
38 // purpose. It is provided "as is" without express or implied warranty.
41 * @file insert_fn_imps.hpp
42 * Contains an implementation class for bin_search_tree_.
46 inline std::pair<typename PB_ASSOC_CLASS_C_DEC::find_iterator, bool>
47 PB_ASSOC_CLASS_C_DEC::
48 insert_leaf(const_mapped_reference r_mapped_value)
50 PB_ASSOC_DBG_ONLY(assert_valid(true, true);)
53 return (std::make_pair(
54 insert_imp_empty(r_mapped_value),
57 node_pointer p_nd = m_p_head->m_p_parent;
58 node_pointer p_pot = m_p_head;
61 if (!Cmp_Fn::operator()(
62 PB_ASSOC_V2F(p_nd->m_value),
63 PB_ASSOC_V2F(r_mapped_value)))
67 p_nd = p_nd->m_p_left;
70 p_nd = p_nd->m_p_right;
72 if (p_pot == m_p_head)
73 return (std::make_pair(
74 insert_leaf_new(r_mapped_value, m_p_head->m_p_right, false),
77 if (!Cmp_Fn::operator()(
78 PB_ASSOC_V2F(r_mapped_value),
79 PB_ASSOC_V2F(p_pot->m_value)))
81 PB_ASSOC_DBG_ONLY(assert_valid(true, true);)
83 PB_ASSOC_DBG_ONLY(my_map_debug_base::check_key_exists(
84 PB_ASSOC_V2F(r_mapped_value)));
86 return (std::make_pair(p_pot, false));
89 PB_ASSOC_DBG_ONLY(my_map_debug_base::check_key_does_not_exist(
90 PB_ASSOC_V2F(r_mapped_value)));
92 p_nd = p_pot->m_p_left;
94 return (std::make_pair(
95 insert_leaf_new(r_mapped_value, p_pot, true),
98 while (p_nd->m_p_right != NULL)
99 p_nd = p_nd->m_p_right;
101 return (std::make_pair(
102 insert_leaf_new(r_mapped_value, p_nd, false),
107 inline typename PB_ASSOC_CLASS_C_DEC::iterator
108 PB_ASSOC_CLASS_C_DEC::
109 insert_leaf_new(const_mapped_reference r_mapped_value, node_pointer p_nd, bool left_nd)
111 node_pointer p_new_nd =
112 get_new_node_for_leaf_insert(r_mapped_value, my_traits_base::s_no_throw_copies_indicator);
116 PB_ASSOC_DBG_ASSERT(p_nd->m_p_left == NULL);
117 PB_ASSOC_DBG_ASSERT(Cmp_Fn::operator()(
118 PB_ASSOC_V2F(r_mapped_value),
119 PB_ASSOC_V2F(p_nd->m_value)));
121 p_nd->m_p_left = p_new_nd;
123 if (m_p_head->m_p_left == p_nd)
124 m_p_head->m_p_left = p_new_nd;
128 PB_ASSOC_DBG_ASSERT(p_nd->m_p_right == NULL);
129 PB_ASSOC_DBG_ASSERT(Cmp_Fn::operator()(
130 PB_ASSOC_V2F(p_nd->m_value),
131 PB_ASSOC_V2F(r_mapped_value)));
133 p_nd->m_p_right = p_new_nd;
135 if (m_p_head->m_p_right == p_nd)
136 m_p_head->m_p_right = p_new_nd;
139 p_new_nd->m_p_parent = p_nd;
141 p_new_nd->m_p_left = p_new_nd->m_p_right = NULL;
143 PB_ASSOC_DBG_ONLY(assert_node_consistent(p_nd));
145 update_to_top(p_new_nd, (Node_Updator* )this);
147 PB_ASSOC_DBG_ONLY(my_map_debug_base::insert_new(
148 PB_ASSOC_V2F(r_mapped_value)));
150 return (iterator(p_new_nd));
154 inline typename PB_ASSOC_CLASS_C_DEC::iterator
155 PB_ASSOC_CLASS_C_DEC::
156 insert_imp_empty(const_mapped_reference r_mapped_value)
158 node_pointer p_new_node =
159 get_new_node_for_leaf_insert(r_mapped_value, my_traits_base::s_no_throw_copies_indicator);
161 m_p_head->m_p_left = m_p_head->m_p_right =
162 m_p_head->m_p_parent = p_new_node;
164 p_new_node->m_p_parent = m_p_head;
166 p_new_node->m_p_left = p_new_node->m_p_right = NULL;
168 PB_ASSOC_DBG_ONLY(my_map_debug_base::insert_new(
169 PB_ASSOC_V2F(r_mapped_value)));
171 update_to_top(m_p_head->m_p_parent, (Node_Updator* )this);
173 return (iterator(p_new_node));
177 inline typename PB_ASSOC_CLASS_C_DEC::node_pointer
178 PB_ASSOC_CLASS_C_DEC::
179 get_new_node_for_leaf_insert(const_mapped_reference r_val, pb_assoc::detail::int_to_type<false>)
181 node_pointer p_new_nd = s_node_allocator.allocate(1);
183 cond_dealtor_t cond(p_new_nd);
185 new (const_cast<void* >(
186 static_cast<const void* >(&p_new_nd->m_value)))
187 typename Node::value_type(r_val);
189 cond.set_no_action();
191 p_new_nd->m_p_left = p_new_nd->m_p_right = NULL;
199 inline typename PB_ASSOC_CLASS_C_DEC::node_pointer
200 PB_ASSOC_CLASS_C_DEC::
201 get_new_node_for_leaf_insert(const_mapped_reference r_val, pb_assoc::detail::int_to_type<true>)
203 node_pointer p_new_nd = s_node_allocator.allocate(1);
205 new (const_cast<void* >(
206 static_cast<const void* >(&p_new_nd->m_value)))
207 typename Node::value_type(r_val);
209 p_new_nd->m_p_left = p_new_nd->m_p_right = NULL;